package com.seqb.manage.reptile.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seqb.common.ResultVo;
import com.seqb.common.utils.ReptileUtils;
import com.seqb.manage.choice.dao.ChoiceQuestionDao;
import com.seqb.manage.choice.domain.ChoiceQuestionDO;
import com.seqb.manage.paper.dao.PaperQuestionDao;
import com.seqb.manage.paper.domain.PaperQuestionDO;
import com.seqb.manage.reptile.service.AnswerReptileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2019/12/9
 */
@Service
public class AnswerReptileServiceImpl implements AnswerReptileService {
    @Autowired
    private ChoiceQuestionDao choiceQuestionDao;
    @Autowired
    private PaperQuestionDao paperQuestionDao;

    @Override
    public ResultVo reptile(Map<String, Object> params) {
        try {
            if (null == params || null == params.get("url") || null == params.get("subject")) {
                return ResultVo.error("参数不足！");
            }
            String url = String.valueOf(params.get("url"));
            String subject = String.valueOf(params.get("subject"));
            String type = "1";

            List list = new ArrayList();
            //信息系统项目管理师
            if ("xxxtxmgls".equals(subject)) {
                if (null != params.get("type")) {
                    type = String.valueOf(params.get("type"));
                }
                if ("1".equals(type) && null == params.get("page")) {
                    return ResultVo.error("参数不足！");
                }
                if ("1".equals(type)) {
                    Integer page = Integer.valueOf(String.valueOf(params.get("page")));
                    list = getXxxtxmglsAnswer(url, type, page);
                } else {
                    list = getXxxtxmglsAnswer(url, type, null);
                }
            }
            return ResultVo.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultVo.error("数据解析错误！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo save(Map<String, Object> params) {
        try {
            if (null == params || null == params.get("url") || null == params.get("subject") || null == params.get("paperId")) {
                return ResultVo.error("参数不足！");
            }
            String url = String.valueOf(params.get("url"));
            String subject = String.valueOf(params.get("subject"));
            Long paperId = Long.valueOf(String.valueOf(params.get("paperId")));
            String type = "1";
            //信息系统项目管理师
            if ("xxxtxmgls".equals(subject)) {
                if (null != params.get("type")) {
                    type = String.valueOf(params.get("type"));
                }
                if ("1".equals(type) && null == params.get("page")) {
                    return ResultVo.error("参数不足！");
                }
                boolean flag;
                if ("1".equals(type)) {
                    Integer page = Integer.valueOf(String.valueOf(params.get("page")));
                    flag = saveXxxtxmglsQuestion(url, type, page, paperId);
                } else {
                    flag = saveXxxtxmglsQuestion(url, type, null, paperId);
                }
                if (!flag) {
                    return ResultVo.error("操作失败！");
                }
            }
            return ResultVo.ok();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ResultVo.error();
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveXxxtxmglsQuestion(String url, String type, Integer page, Long paperId) {
        try {
            if ("1".equals(type)) {
                //1、选择题
                List answerList = ReptileUtils.testGetXgwXztAnswer(url, page);

                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("paper_id", paperId);
                List<PaperQuestionDO> questionList = paperQuestionDao.selectList(queryWrapper);
                for (int i = 0; i < answerList.size(); i++) {
                    String answer = String.valueOf(((Map) answerList.get(i)).get("answer"));
                    String sortNumber = String.valueOf(((Map) answerList.get(i)).get("sortNumber"));
                    for (int j = 0; j < questionList.size(); j++) {
                        PaperQuestionDO paperQuestion = questionList.get(j);
                        if (String.valueOf(paperQuestion.getSortNumber()).equals(sortNumber)) {
                            Long questionId = paperQuestion.getQuestionId();
                            ChoiceQuestionDO choiceQuestion = choiceQuestionDao.selectById(questionId);
                            if (choiceQuestion == null) {
                                break;
                            }
                            ChoiceQuestionDO updateDO = new ChoiceQuestionDO();
                            updateDO.setId(choiceQuestion.getId());
                            updateDO.setAnswer(answer);
                            choiceQuestionDao.updateById(updateDO);
                            break;
                        }
                    }
                }

            } else if ("2".equals(type)) {
                //2、案例分析

            } else if ("3".equals(type)) {
                //3、论文

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return false;
    }

    //获取信息系统项目管理师试题
    private List getXxxtxmglsAnswer(String url, String type, Integer page) {
        List list = new ArrayList();
        if ("1".equals(type)) {
            list = ReptileUtils.testGetXgwXztAnswer(url, page);

        } else if ("2".equals(type)) {


        } else if ("3".equals(type)) {

        }
        return list;
    }

}
